package me.zhengjie.modules.serviceprovider.rest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.ObjectArrayCodec;
import me.zhengjie.aop.log.Log;
import me.zhengjie.modules.serviceprovider.domain.TServiceproviderInfo;
import me.zhengjie.modules.serviceprovider.service.TServiceproviderInfoService;
import me.zhengjie.modules.serviceprovider.service.dto.TServiceproviderInfoQueryCriteria;
import me.zhengjie.utils.HttpResponseUtils;
import me.zhengjie.utils.SecurityUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;


/**
* @author xuyukun
* @date 2019-07-20
*/
@RestController
@RequestMapping("api")
public class TServiceproviderInfoController {

    @Autowired
    private TServiceproviderInfoService tServiceproviderInfoService;

    @Log("查询TServiceproviderInfo")
    @GetMapping(value = "/tServiceproviderInfo")
    //@PreAuthorize("hasAnyRole('ADMIN','TSERVICEPROVIDERINFO_ALL','TSERVICEPROVIDERINFO_SELECT')")
    public ResponseEntity getTServiceproviderInfos(TServiceproviderInfoQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity(tServiceproviderInfoService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @Log("查询TServiceproviderInfo,仅返回标识名字")
    @GetMapping(value = "/tServiceproviderInfo/queryidandname")
    //@PreAuthorize("hasAnyRole('ADMIN','TSERVICEPROVIDERINFO_ALL','TSERVICEPROVIDERINFO_SELECT')")
    public ResponseEntity queryServiceproviderInfos(HttpServletRequest request){
        String str =  request.getParameter("level") ;
        ArrayList<String> level = new ArrayList<>();
        if(str != null) {
            if(str.equals("初级")) {
                level.add("初级");
                level.add("中级");
                level.add("高级");
            }
            if(str.equals("中级")) {
                level.add("中级");
                level.add("高级");
            }
            if(str.equals("高级")) {
                level.add("高级");
            }
        }else{
            level.add("初级");
            level.add("中级");
            level.add("高级");
        }
        java.util.List<Object[]> infos = tServiceproviderInfoService.queryServiceproviderInfo(
                request.getParameter("major"),
                level,
                request.getParameter("country"),
                request.getParameter("province"),
                request.getParameter("city"),
                request.getParameter("county"));
        JSONArray array = new JSONArray();
        for(Object[] value: infos) {
            JSONObject obj = new JSONObject();
            obj.put("id", value[0]);
            obj.put("seproName",value[1]);
            array.add(obj);
        }
        return new ResponseEntity(array, HttpStatus.OK);
    }

    @GetMapping(value = "/tServiceproviderInfo/evaluate")
    //@PreAuthorize("hasAnyRole('ADMIN','TSERVICEPROVIDERINFO_ALL','TSERVICEPROVIDERINFO_SELECT')")
    public ResponseEntity getProviderEvaluates(TServiceproviderInfoQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity(tServiceproviderInfoService.queryProviderEvaluates(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/tServiceproviderInfo/export")
    public void getProviderEvaluates(TServiceproviderInfoQueryCriteria criteria,HttpServletResponse response){
        XSSFWorkbook wb = tServiceproviderInfoService.exportEvaluate(criteria);
        try{
            ServletOutputStream output = response.getOutputStream();
            response.setHeader("Content-disposition", "attachment; filename=Info.xls");
            response.setContentType("application/msexcel");
            wb.write(output);
            output.close();
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }


    
    @Log("查询TServiceproviderInfoAll")
    @GetMapping(value = "/tServiceproviderInfoAll")
    //@PreAuthorize("hasAnyRole('ADMIN','TSERVICEPROVIDERINFO_ALL','TSERVICEPROVIDERINFO_SELECT')")
    public ResponseEntity getTServiceproviderInfoAll(){
        TServiceproviderInfoQueryCriteria criteria =new TServiceproviderInfoQueryCriteria();
        return new ResponseEntity(tServiceproviderInfoService.queryAll(criteria),HttpStatus.OK);
    }
    
    @Log("新增TServiceproviderInfo")
    @PostMapping(value = "/tServiceproviderInfo")
    @PreAuthorize("hasAnyRole('ADMIN','TSERVICEPROVIDERINFO_ALL','TSERVICEPROVIDERINFO_CREATE')")
    public ResponseEntity create(@Validated @RequestBody TServiceproviderInfo resources){
        return new ResponseEntity(tServiceproviderInfoService.create(resources),HttpStatus.CREATED);
    }

    @Log("修改TServiceproviderInfo")
    @PutMapping(value = "/tServiceproviderInfo")
    //@PreAuthorize("hasAnyRole('ADMIN','TSERVICEPROVIDERINFO_ALL','TSERVICEPROVIDERINFO_EDIT')")
    public ResponseEntity update(@Validated @RequestBody TServiceproviderInfo resources){
        tServiceproviderInfoService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @Log("审核TServiceproviderInfo")
    @PostMapping(value = "/tServiceproviderInfo/verify")
    //@PreAuthorize("hasAnyRole('ADMIN','TSERVICEPROVIDERINFO_ALL','TSERVICEPROVIDERINFO_EDIT')")
    public ResponseEntity verify(@Validated @RequestBody JSONObject obj){
        Long id = obj.getLong("id");
        Integer state = obj.getInteger("state");
        String seproId = obj.getString("seproId");
        Long major = 0L;
        if(seproId != null)
            major = Long.valueOf(seproId);
        if(state.intValue() != 1 && state.intValue() != 2) {
            return HttpResponseUtils.passDefaultResponse("状态值有误", HttpStatus.BAD_REQUEST) ;
        }
        String reason = obj.getString("reason");
        tServiceproviderInfoService.verify(id, state, reason,major);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @Log("删除TServiceproviderInfo")
    @DeleteMapping(value = "/tServiceproviderInfo/{id}")
    //@PreAuthorize("hasAnyRole('ADMIN','TSERVICEPROVIDERINFO_ALL','TSERVICEPROVIDERINFO_DELETE')")
    public ResponseEntity delete(@PathVariable Long id){
        try {
            tServiceproviderInfoService.delete(id);
            return new ResponseEntity(HttpStatus.OK);
        }catch (Exception ex) {
            ex.printStackTrace();
            return HttpResponseUtils.passDefaultResponse(ex);
        }
    }


    @Log("修改企业邀请码TServiceproviderManager")
    @RequestMapping(value = "/tServiceproviderInfo/resetinvitecode")
    //@PreAuthorize("hasAnyRole('ADMIN','TSERVICEPROVIDERPERSON_ALL','TSERVICEPROVIDERPERSON_CREATE')")
    public ResponseEntity resetinviteCode(@RequestBody JSONObject resources) {
        Long id = resources.getLong("id");
        //String inviteCode = resources.getString("inviteCode");
        String inviteCode = tServiceproviderInfoService.resetInviteCode(id, "") ;
        return  HttpResponseUtils.passDefaultResponse(inviteCode, HttpStatus.OK);
    }
}